<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <title>草稿编辑</title>
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../../css/global.css" media="all"/>
</head>
<body style="background-color: #f5f5f5">
<div class="panel-page">
    <div class="top-box">
        问题录入 - 草稿箱
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <form class="layui-form form-container" style="width: 1100px;margin: 0 auto" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">巡察单位</label>
                    <div class="layui-input-inline">
                        <select name="branch" lay-filter="branch" id="branch" lay-search lay-verify="required">
                            <option value=""></option>
                        </select>
                    </div>

                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">审核人</label>
                    <div class="layui-input-inline">
                        <select name="reviewer" id="reviewer" lay-verify="required" lay-filter="xmFilter" class="xg">
                        </select>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">

                <div class="layui-inline">
                    <label class="layui-form-label">督察形式</label>
                    <div class="layui-input-inline">
                        <select name="year" id="year" lay-verify="required" lay-filter="xmFilter" class="xg">
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">问题类别</label>
                    <div class="layui-input-inline">
                        <select name="questionType" id="questionType" lay-verify="required" lay-filter="xmFilter">
                            <option value=""></option>
                        </select>
                    </div>
                </div>

            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">问题来源</label>
                    <div class="layui-input-inline">
                        <select name="sourceProblem" id="sourceProblem" lay-verify="required" lay-filter="xmFilter">
                            <option value=""></option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">问题项</label>
                <div class="layui-input-block" style="margin-left: 150px;width: 740px">
                    <textarea placeholder="请输入问题项" maxlength="100" id="question" name="question" class="layui-textarea"
                              style="height: 100px"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">问题描述</label>
                <div class="layui-input-block" style="margin-left: 150px;width: 740px">
                    <textarea placeholder="请输入问题描述" name="questionDetail" id="questionDetail" class="layui-textarea"
                              style="height: 150px"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="saveBtn">提交</button>

                    <button type="reset" class="layui-btn layui-btn-primary" onclick="window.close()">取消</button>
                    <button type="button" class="layui-btn layui-btn-sm" id="test3"><i class="layui-icon"></i>上传附件
                    </button>

                </div>
            </div>
        </form>

    </div>
</div>
<script type="text/javascript" src="../../js/libs/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/my/permission.js"></script>
<script type="text/javascript">
    $('#btn2').attr('disabled', true)
    PanelInit()

    function PanelInit() {
        setOption()
    }

    //获取url后的参数值
    function getUrlParam(key) {
        var href = window.location.href;
        var url = href.split("?");
        if (url.length <= 1) {
            return "";
        }
        var params = url[1].split("&");

        for (var i = 0; i < params.length; i++) {
            var param = params[i].split("=");
            if (key == param[0]) {
                return param[1];
            }
        }
    }

    //刷新表单
    function renderForm() {
        layui.use('form', function () {
            var form = layui.form; //高版本建议把括号去掉，有的低版本，需要加()
            form.render();
        });
    }

    /*初始化下拉*/
    function setOption() {
        // 例如：new Option(item.xm, item.id)第一个参数是下拉列表中显示的值  第二个参数是选中传递给后台的值
        layui.use(['form', 'upload', 'layer'], function () {
                var year, questionKeyword, questionType, branch, schedule, sourceProblem, reviewer;
                var form = layui.form;
                /*单位*/
                branch = $.ajax({
                    url: '/dropDown/getBranchByUserId',
                    dataType: 'json',
                    type: 'post',
                    async: false,
                    success: function (data) {
                        // 使用循环遍历，给下拉列表赋值
                        $.each(data, function (index, value) {
                            $('#branch').append(new Option(value.value, value.key));// 下拉菜单里添加元素
                        });
                        form.render("select");//重新渲染 固定写法
                        console.log("单位");
                    }
                })
                    /*巡查年度*/
                    , year = $.ajax({
                    url: '/dropDown/getYear',
                    dataType: 'json',
                    type: 'post',
                    success: function (data) {
                        // 使用循环遍历，给下拉列表赋值
                        $.each(data, function (index, value) {
                            $('#year').append(new Option(value.value, value.value));// 下拉菜单里添加元素
                        });
                        form.render();//重新渲染 固定写法
                    }
                })
                    /*类别*/
                    , questionType = $.ajax({
                    url: '/dropDown/getQuestion',
                    dataType: 'json',
                    type: 'post',
                    async: false,
                    success: function (data) {
                        // 使用循环遍历，给下拉列表赋值
                        $.each(data, function (index, value) {
                            $('#questionType').append(new Option(value.value, value.value));// 下拉菜单里添加元素
                        });
                        form.render("select");//重新渲染 固定写法
                        console.log("类别");
                    }
                })
                    /*关键字*/
                    , questionKeyword = $.ajax({
                    url: '/dropDown/getQuestionKeyword',
                    dataType: 'json',
                    type: 'post',
                    async: false,
                    success: function (data) {
                        // 使用循环遍历，给下拉列表赋值
                        $.each(data, function (index, value) {
                            $('#questionKeyword').append(new Option(value.value, value.value));// 下拉菜单里添加元素
                        });
                        form.render("select");//重新渲染 固定写法
                        console.log("关键字");
                    }
                })
                    /*问题来源*/
                    , sourceProblem = $.ajax({
                    url: '/dropDown/getSourceProblem',
                    dataType: 'json',
                    type: 'post',
                    async: false,
                    success: function (data) {
                        // 使用循环遍历，给下拉列表赋值
                        $.each(data, function (index, value) {
                            $('#sourceProblem').append(new Option(value.value, value.value));// 下拉菜单里添加元素
                        });
                        form.render("select");//重新渲染 固定写法
                        console.log("问题来源");
                    }
                })
                    /*审核人*/
                    , reviewer = $.ajax({
                    url: '/dropDown/getReviewer/' + branch,
                    dataType: 'json',
                    type: 'post',
                    async: false,
                    success: function (data) {
                        if (data.length == 0) {
                            layer.msg("该公司没有审核人员！", {
                                icon: 2,
                                time: 1000
                            })
                        }
                        $("#reviewer").empty();
                        // 使用循环遍历，给下拉列表赋值
                        $.each(data, function (index, value) {
                            $('#reviewer').append(new Option(value.value, value.value));// 下拉菜单里添加元素
                        });
                        renderForm();//重新渲染 固定写法
                        console.log("审核人");
                    }
                })
                    /*进度*/
                    , schedule = $.ajax({
                    url: '/dropDown/getSchedule',
                    dataType: 'json',
                    type: 'post',
                    async: false,
                    success: function (data) {
                        // 使用循环遍历，给下拉列表赋值
                        $.each(data, function (index, value) {
                            $('#schedule').append(new Option(value.value, value.value));// 下拉菜单里添加元素
                        });
                        form.render("select");//重新渲染 固定写法
                        console.log("进度");
                    }
                })
                    , form.on('select(branch)', function () {
                    if ($("#branch option:selected").text() === null || $("#branch option:selected").text() === '') {
                        $('#test3').attr('disabled', true)
                    } else {
                        $('#test3').attr('disabled', false)
                    }
                });
                $.when(branch, year, questionType, questionKeyword, sourceProblem, reviewer, schedule).done(function () {
                    setFormValue()
                });
            }
        );
    }

    function setFormValue() {
        // 数据回显
        layui.use(['layer', 'form'], function () {
            var id = getUrlParam("id");
            if (id > 0) {
                $.ajax({
                    type: 'get',
                    url: '/mOverseeQuestions/' + id,
                    async: false,
                    success: function (data) {
                        console.log(data)
                        $("#question").val(data.question);
                        $("#questionId").val(data.questionId);
                        $("#questionDetail").val(data.questionDetail);
                        $("#remark").val(data.remark);
                        $("#startPatrolTime").val(data.startPatrolTime);
                        $("#liaison").val(data.liaison);
                        /*下拉*/
                        $("#branch").val(data.branchId);
                        $("#year").val(data.startPatrolTime);
                        $("#questionType").val(data.questionType);
                        $("#questionKeyword").val(data.questionKeyword);
                        $("#sourceProblem").val(data.sourceProblem);
                        reviewerAjax(data.branchId);
                        $("#reviewer").val(data.reviewer);
                        renderForm();
                    }
                });
            }
        });
    }

    layui.use(['form', 'laydate'], function () {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#startPatrolTime'
            , range: true,
            done: function (value, date, endDate) {

            }
        });

    });

    var path = '';
    layui.use('upload', function () {
        var upload = layui.upload;
        //执行实例
        var uploadInst = upload.render({
            elem: '#test3' //绑定元素
            , url: '/mOverseeQuestions/uploadSource' //上传接口
            , accept: 'file'
            , multiple: true
            , method: 'POST',
            data: {
                branch: $("#branch option:selected").text()
            },
            done: function (res) {
                //上传完毕回调
                layer.msg("上传成功！");
                $("#test3").val(res.msg);
            }
            , error: function (res) {
                //请求异常回调
                layer.msg("上传失败！");
            }
        });
    });
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var id = getUrlParam("id");
            data.field.id = id;
            data.field.branchId = $("#branch option:selected").val();//获取文本 id: 以降序排列此列
            data.field.branch = $("#branch option:selected").text();//获取文本 id: 以降序排列此列
            data.field.questionKeyword = $("#questionKeyword option:selected").text();//获取文本
            data.field.questionType = $("#questionType option:selected").text();//获取文本
            data.field.startPatrolTime = $("#year option:selected").text();//获取文本
            data.field.file = $("#test3").val();
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: '/mOverseeQuestions/draftsSave',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data.field),
                success: function (res) {
                    console.log(res);
                    if (res.errorCode == 0) {
                        layer.msg("保存成功", {
                            icon: 1,
                        }, function () {
                            window.close();
                        });
                    }
                }
            })
            return false;
        })
        ;


    });

</script>
</body>
</html>



